
<template>
  <div>
    <el-dialog
        title="批量添加教师"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <div class="header">
        <el-upload
            class="upload-demo"
            action=""
            :on-change="handleFileSelect"
            :auto-upload="false">
          <el-button type="primary" >点击上传</el-button>
        </el-upload>
        <el-button type="primary" style="float: right;margin-bottom: 50px" @click="submit">确定提交</el-button>
      </div>
      <span>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
           <el-table-column
               prop="account"
               label="工号"
               width="150">
        </el-table-column>
        <el-table-column
            prop="name"
            label="姓名">
        </el-table-column>
        <el-table-column
            prop="gender"
            label="性别">
        </el-table-column>
        <el-table-column
            prop="nation"
            label="民族">
        </el-table-column>
</el-table>
  </span>
    </el-dialog>
  </div>
</template>
<script>
import {_addBatchTeacher} from '../../../../api/super_admin'
import {read, SSF, utils} from 'xlsx'

export default {
  name: 'TeacherAddMany',
  data () {
    return {
      fileList: [], // 上传文件列表
      tableHead: [], // 表头
      tableData: [], // 表数据
      dialogVisible: false
    }
  },
  methods: {
    show () {
      this.dialogVisible = true
    },
    handleFileSelect (event) {
      const file = event.raw
      const reader = new FileReader()
      // 处理文件内容
      reader.onload = (e) => {
        const data = new Uint8Array(e.target.result)
        const workbook = read(data, { type: 'array' })
        const worksheet = workbook.Sheets[workbook.SheetNames[0]]
        const jsonData = utils.sheet_to_json(worksheet, { header: 1 })
        // 提取文件内容
        let headers = jsonData[0]
        jsonData.slice(1).forEach(row => {
          const rowData = row.reduce((accumulator, item, index) => {
            let column = headers[index]
            // 检查 column 是否为字符串类型
            if (typeof column === 'string' && column.toLowerCase().includes('date')) {
              // 继续处理日期逻辑
              const date = SSF.parse_date_code(item)
              accumulator[column] = `${date.y}-${date.m}-${date.d}`
            } else {
              accumulator[column] = item
            }
            return accumulator
          }, {})
          this.tableData.push(rowData)
        })
      }
      // 读取文件内容
      reader.readAsArrayBuffer(file)
      console.log(this.tableData)
    },
    submit () {
      for (let i = 0; i < this.tableData; i++) {
        for (let item in this.tableData[i]) {
          if (item === '') {
            this.$message({
              message: '第' + i + '个教师存在空字段',
              type: 'error'
            })
            return
          }
        }
      }
      this.$confirm('确定批量添加这些教师？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _addBatchTeacher(this.tableData).then(res => {
          if (res.status) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.dialogVisible = false
            this.$emit('ok')
          } else {
            this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        }
        )
      }).catch(() => {

      })
    }
  }
}
</script>
<style scoped>
</style>
